package com.lanhai.zhonglai.dispatch;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lanhai.zhonglai.MsgCore;
import com.lanhai.zhonglai.beans.AgvBean;
import com.lanhai.zhonglai.beans.AgvMountBean;
import com.lanhai.zhonglai.beans.DotBean;
import com.lanhai.zhonglai.beans.StationBean;
import com.lanhai.zhonglai.dao.RoadDao;
import com.lanhai.zhonglai.dao.StationGroupDao;
import com.lanhai.zhonglai.enums.AgvMountTypeEnum;
import com.lanhai.zhonglai.enums.CacheStationEnumType;
import com.lanhai.zhonglai.interfaces.IAgv;
import com.lanhai.zhonglai.interfaces.IAgvEvent;
import com.lanhai.zhonglai.interfaces.IDot;
import com.lanhai.zhonglai.interfaces.IRoad;
import com.lanhai.zhonglai.interfaces.ISendData;
import com.lanhai.zhonglai.interfaces.IStation;
import com.lanhai.zhonglai.interfaces.IStationMount;
import com.lanhai.zhonglai.interfaces.IStationProtocol;

/**
 * 机台离开的判断
 *
 * @author zhengwei
 */
@Component
public class StationLeaveCheck {
    @Autowired
    IAgv iAgv;

    @Autowired
    IStation iStation;

    @Autowired
    ISendData sendTools;

    @Autowired
    IAgvEvent iAgvEvent;

    @Autowired
    IStationMount iStationMount;

    @Autowired
    IRoad iRoad;


    @Autowired
    IDot iDot;

    // private static final Logger agvlog = LogManager.getLogger("agvlog");

    /**
     * 返回是否处理
     *
     * @param agv
     * @param dot
     * @param amount
     * @return
     */
    public void check(DotBean dot, AgvBean agv, StationBean station, IStationProtocol stationEvent,
                      AgvMountBean amount) {

        if (station.getCmdSign().equals("ZRX")) {
            leaveZRX(dot, agv, station, stationEvent, amount);
        } else if (station.getCmdSign().equals("ZRS")) {
            leaveZRS(dot, agv, station, stationEvent, amount);
        } else if (station.getCmdSign().equals("KS")) {
            if (station.getProcess().contains("上")) {
                leaveKSS(dot, agv, station, stationEvent, amount);
            } else {
                leaveKSX(dot, agv, station, stationEvent, amount);
            }
        } else if (station.getCmdSign().equals("POPAID")) {
            leavePOPAID(dot, agv, station, stationEvent, amount);
        } else if (station.getCmdSign().equals("TH")) {
            if (station.getProcess().contains("上")) {
                leaveTHS(dot, agv, station, stationEvent, amount);
            } else {
                leaveTHX(dot, agv, station, stationEvent, amount);
            }

        } else if (station.getCmdSign().equals("ALD")) {
            if (station.getProcess().contains("干")) {
                leaveALDG(dot, agv, station, stationEvent, amount);
            } else {
                leaveALDS(dot, agv, station, stationEvent, amount);
            }
        } else if (station.getCmdSign().equals("JP")) {
            if (station.getProcess().contains("上")) {
                leaveJPS(dot, agv, station, stationEvent, amount);
            } else {
                leaveJPX(dot, agv, station, stationEvent, amount);
            }

        } else if (station.getCmdSign().equals("SW")) {
            leaveSW(dot, agv, station, stationEvent, amount);
        } else if (station.getCmdSign().equals("BSG")) {
            leaveBSG(dot, agv, station, stationEvent, amount);
        } else if (station.getCmdSign().equals("BOE")) {
            if (station.getProcess().contains("上")) {
                leaveBOES(dot, agv, station, stationEvent, amount);
            } else {
                leaveBOEX(dot, agv, station, stationEvent, amount);
            }
        } else if (station.getCmdSign().equals("CB")) {
            leaveCB(dot, agv, station, stationEvent, amount);
        } else if (station.getCmdSign().equals("ZM")) {
            if (station.getProcess().contains("上")) {
                leaveZMS(dot, agv, station, stationEvent, amount);
            } else {
                leaveZMX(dot, agv, station, stationEvent, amount);
            }
        } else if (station.getCmdSign().equals("BM")) {
            if (station.getProcess().contains("上")) {
                leaveBMS(dot, agv, station, stationEvent, amount);
            } else {
                leaveBMX(dot, agv, station, stationEvent, amount);
            }
        }

    }


    /**
     * 清理各种状态，然后回退到二维码，让二维码事件去处理
     *
     * @param agv
     * @param dot             当前点位
     * @param nextProcess     下道工序
     * @param nextStationName 目标机台名称（可能有）
     * @param nextRoad        路由路线
     * @param station         当前机台
     * @param stationEvent    当前机台事件（离开指令）
     */
    void toQr(AgvBean agv, DotBean dot, String nextProcess, String nextStationName, int nextSpecialline, int nextRoad
            , StationBean station, IStationProtocol stationEvent, boolean isHC) {

        //iStation.unlockForAgv(agv.getAgvId());
        //iStation.unlockStationForDot(dot.getDotno(), agv.getAgvId());

        // 保存小车状态
        agv.setTransing(false);
        agv.setFinishTrans(true);
        agv.setUpHasTrans(false);
        agv.setDownHasTrans(false);
        agv.setNextRoad(nextRoad);
        agv.setNexttStationName(nextStationName);
        agv.setNextProcess(nextProcess);
        agv.setSpecialline(nextSpecialline);
        agv.setToHC(isHC);
        iAgv.saveCache(agv);

        if (nextStationName != null && nextStationName.equals("") == false) {
            if (nextSpecialline > 0) {
                MsgCore.sendMsg(agv.getAgvId(), "目标机台：" + nextStationName + ",生产线：" + nextSpecialline);
            } else {
                MsgCore.sendMsg(agv.getAgvId(), "目标机台：" + nextStationName);
            }

        } else {
            MsgCore.sendMsg(agv.getAgvId(), "寻找下一道工序");
        }

        // 清空机台装料情况（不是解锁机台）
        iStationMount.clear(dot.getDotno());
        // 让小车完成传输，退回二维码处
        iAgvEvent.finishTrans(agv);

        // 向机台发送离开的信号
        byte[] cmd = stationEvent.agvLeave(station.getLine(), station.getFillType());
        sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV离开", cmd,
                station.getStationname());

    }

    // ***************** 各工序的处理 ********

    /**
     * 制绒上料，没有定线
     *
     * @param agv
     * @param station
     * @param stationEvent
     */
    void leaveZRS(DotBean dot, AgvBean agv, StationBean station, IStationProtocol stationEvent, AgvMountBean amount) {
        if (agv.getAgvId() == 19 || agv.getAgvId() == 20) {
            int qr = iDot.getQrDotNo(station.getDotno());
            int road = iRoad.getNormalRoad(qr, 363);
            if (road > 0) {
                toQr(agv, dot, "", "", 0, road, station, stationEvent, false);
            } else {
                MsgCore.sendMsg(agv.getAgvId(), "缺少到363的路线");
                toQr(agv, dot, "", "", 0, 0, station, stationEvent, false);
            }
        } else {
            toQr(agv, dot, "", "", 0, 0, station, stationEvent, false);
        }
    }

    /**
     * 制绒下料，目标有定线
     *
     * @param dot
     * @param agv
     * @param station
     * @param stationEvent
     * @param amount
     */
    void leaveZRX(DotBean dot, AgvBean agv, StationBean station, IStationProtocol stationEvent, AgvMountBean amount) {
        String nextProcess = "扩散上料";
        //只能fetch，不能用hasEmpty。扩散上下机台都有
        //如果是多机台，可以直接上锁
        StationGroupDao gbean = iStation.fetchNextInStation(406, agv.getAgvId(), nextProcess, station.getSpecialline(), false);

        if (gbean == null) {
            //不锁缓存机
            boolean tocache = iStation.findCache(CacheStationEnumType.ZRX, agv.getAgvId(), true, false);
            if (tocache) {
                // 去406
                toQr(agv, dot, nextProcess, CacheStationEnumType.ZRX.getName(), 0, 0, station, stationEvent, true);
            } else {
                MsgCore.sendMsg(agv.getAgvId(), String.format("%s没有可用的机台，等待中", nextProcess));
            }
        } else {
            // 不锁机台，只是去406
            toQr(agv, dot, nextProcess, gbean.getStationname(), 0, gbean.getSpecialline(), station, stationEvent,
                    false);
        }
    }

    /**
     * 扩散上料。目标没有定线
     *
     * @param dot
     * @param agv
     * @param station
     * @param stationEvent
     * @param amount
     */
    void leaveKSS(DotBean dot, AgvBean agv, StationBean station, IStationProtocol stationEvent, AgvMountBean amount) {
        //此处AGV是空车
        //扩散上需要立即解锁，唯一特殊的机台
        iStation.unlockStationForDot(dot.getDotno(), agv.getAgvId());


        String nextProcess = "扩散下料";
        StationGroupDao gbean = iStation.fetchNextOutStation(station.getDotno(), agv.getAgvId(), nextProcess, true);

        if (gbean == null) {
            // 没有可用的下料机台，退回二维码,让二维码去处理
            //没有特殊路线，也没有定线
            toQr(agv, dot, nextProcess, "", 0, 0, station, stationEvent, false);
        } else {
            // 锁目标机台，然后走路由(406开始的）
            RoadDao road = iRoad.getTransRoad(406, gbean.getDotno());
            if (road != null && road.getId() > 0) {
                //没有特殊路线，空车定线
                toQr(agv, dot, nextProcess, gbean.getStationname(), road.getId(), 0, station, stationEvent, false);
            } else {
                MsgCore.sendMsg(agv.getAgvId(), String.format("%d与%d没有路线", 406, gbean.getDotno()));

            }
        }
    }

    /**
     * 扩散下料，找BSG，有定线
     *
     * @param dot
     * @param agv
     * @param station
     * @param stationEvent
     * @param amount
     */
    void leaveKSX(DotBean dot, AgvBean agv, StationBean station, IStationProtocol stationEvent, AgvMountBean amount) {
        //上空下满
        String nextProcess = "BSG";
        StationGroupDao gbean = iStation.fetchNextInStation(dot.getDotno(), agv.getAgvId(), nextProcess, station.getSpecialline(), true);

        if (gbean == null) {
            boolean tocache = iStation.findCache(CacheStationEnumType.KS, agv.getAgvId(), true, true);
            if (tocache) {
                // 可以去缓存
                RoadDao road = iRoad.getTransRoad(406, CacheStationEnumType.KS.getDotno());
                if (road != null && road.getId() > 0) {
                    //有路才锁定
                    toQr(agv, dot, nextProcess, CacheStationEnumType.KS.getName(), road.getId(), station.getSpecialline(), station, stationEvent, false);
                } else {
                    MsgCore.sendMsg(agv.getAgvId(), String.format("%d与%d没有路线", 406, CacheStationEnumType.KS.getDotno()));
                }
            } else {
                MsgCore.sendMsg(agv.getAgvId(), String.format("%s没有可用的机台，等待中", nextProcess));
            }
        } else {
            //没有特殊路线，但是有定线
            toQr(agv, dot, nextProcess, gbean.getStationname(), 0, gbean.getSpecialline(), station, stationEvent, false);
        }
    }

    /**
     * BSG,直接走路由
     *
     * @param dot
     * @param agv
     * @param station
     * @param stationEvent
     * @param amount
     */
    void leaveBSG(DotBean dot, AgvBean agv, StationBean station, IStationProtocol stationEvent, AgvMountBean amount) {
        //没有特殊路线，也没有定线
        if (amount.getMountType() == AgvMountTypeEnum.AllEmpty) {
            //空车，只完成上料操作，只需要走路由
            toQr(agv, dot, "制绒下料", "", 0, 0, station, stationEvent, false);
        } else if (amount.getMountType() == AgvMountTypeEnum.UpFullAndDownEmpty) {
            //上满下空，完成了收料，走路由，没有线别，交由437去处理
            toQr(agv, dot, "制绒下料", "", 0, 0, station, stationEvent, false);
        } else {
            MsgCore.sendMsg(agv.getAgvId(), "错误的装料状态");
        }

    }

    /**
     * 碱抛下料，找POPAID上料并锁定，有线别
     *
     * @param dot
     * @param agv
     * @param station
     * @param stationEvent
     * @param amount
     */
    void leaveJPX(DotBean dot, AgvBean agv, StationBean station, IStationProtocol stationEvent, AgvMountBean amount) {
        String nextProcess = "POPAID上料";
        //注意，有线别
        StationGroupDao gbean = iStation.fetchNextInStation(dot.getDotno(), agv.getAgvId(), nextProcess, station.getSpecialline(), true);

        if (gbean == null) {
            boolean tocache = iStation.findCache(CacheStationEnumType.JP, agv.getAgvId(), true, true);
            if (tocache) {
                // 可以去缓存
                RoadDao road = iRoad.getTransRoadFromStation(station.getDotno(), CacheStationEnumType.JP.getDotno());
                if (road != null && road.getId() > 0) {
                    //有路才锁定
                    toQr(agv, dot, nextProcess, CacheStationEnumType.JP.getName(), road.getId(), station.getSpecialline(), station, stationEvent, true);
                } else {
                    MsgCore.sendMsg(agv.getAgvId(), String.format("%d与%d没有路线", 368, CacheStationEnumType.KS.getDotno()));
                }
            } else {
                MsgCore.sendMsg(agv.getAgvId(), String.format("%s没有可用的机台，等待中", nextProcess));
            }
        } else {
            //走路由去368
            toQr(agv, dot, nextProcess, gbean.getStationname(), 0, gbean.getSpecialline(), station, stationEvent, false);
        }
    }

    /**
     * 碱抛上料，直接走路由
     *
     * @param dot
     * @param agv
     * @param station
     * @param stationEvent
     * @param amount
     */
    void leaveJPS(DotBean dot, AgvBean agv, StationBean station, IStationProtocol stationEvent, AgvMountBean amount) {
        //没有特殊路线，也没有定线
        toQr(agv, dot, "碱抛下料", "", 0, 0, station, stationEvent, false);
    }

    /**
     * 如果出料完成，下一步去退火上料锁定，走路由，否则不动 如果是入料完成，空车，去POPAID下料锁定，否则走路由。
     *
     * @param dot
     * @param agv
     * @param station
     * @param stationEvent
     * @param amount
     */
    void leavePOPAID(DotBean dot, AgvBean agv, StationBean station, IStationProtocol stationEvent,
                     AgvMountBean amount) {
        //TODO 找缓存机
        if (station.getProcess().equals("POPAID下料")) {
            // 出料完成，上空下满
            String nextProcess = "退火上料";
            StationGroupDao gbean = iStation.fetchNextInStation(dot.getDotno(), agv.getAgvId(), nextProcess, station.getSpecialline(), true);

            if (gbean == null) {
                MsgCore.sendMsg(agv.getAgvId(), String.format("%s没有可用的机台，等待中", nextProcess));
            } else {
                //走路由，有定线
                toQr(agv, dot, nextProcess, gbean.getStationname(), 0, gbean.getSpecialline(), station, stationEvent, false);
            }

        } else {
            // 上料完成，空车，去下料口
            String nextProcess = "POPAID下料";
            StationGroupDao gbean = iStation.fetchNextOutStation(station.getDotno(), agv.getAgvId(), nextProcess, true);

            if (gbean == null) {
                // 没有机台，走路由
                //没有特殊路线，也没有定线
                toQr(agv, dot, "", "", 0, 0, station, stationEvent, false);
            } else {

                RoadDao road = iRoad.getTransRoad(368, gbean.getDotno());
                if (road != null && road.getId() > 0) {
                    //没有特殊路线，空车，没有定线
                    toQr(agv, dot, nextProcess, gbean.getStationname(), road.getId(), 0, station, stationEvent, false);
                } else {
                    MsgCore.sendMsg(agv.getAgvId(), String.format("%d与%d没有路线", 368, gbean.getDotno()));

                }
            }
        }
    }

    /**
     * 退火上料，找退火下料并锁定，否则走路由 特殊处理
     *
     * @param dot
     * @param agv
     * @param station
     * @param stationEvent
     * @param amount
     */
    void leaveTHS(DotBean dot, AgvBean agv, StationBean station, IStationProtocol stationEvent, AgvMountBean amount) {
        //空车
        String nextProcess = "退火下料";
        StationGroupDao gbean = iStation.fetchNextOutStation(station.getDotno(), agv.getAgvId(), nextProcess, true);

        if (gbean == null) {
            // 没有可用的下料机台，退回二维码,让二维码去处理
            //没有特殊路线，也没有定线
            toQr(agv, dot, "", "", 0, 0, station, stationEvent, false);
        } else {
            RoadDao road = iRoad.getTransRoad(479, gbean.getDotno());
            if (road != null && road.getId() > 0) {
                //有特殊路线，但没有定线
                toQr(agv, dot, nextProcess, gbean.getStationname(), road.getId(), 0, station, stationEvent, false);
            } else {
                MsgCore.sendMsg(agv.getAgvId(), String.format("%d到%d缺少路线", 479, gbean.getDotno()));
            }
        }
    }

    /**
     * 退火下料，去BOE上料，如果有则锁，否则不动
     *
     * @param dot
     * @param agv
     * @param station
     * @param stationEvent
     * @param amount
     */
    void leaveTHX(DotBean dot, AgvBean agv, StationBean station, IStationProtocol stationEvent, AgvMountBean amount) {

        String nextProcess = "BOE上料";
        StationGroupDao gbean = iStation.fetchNextInStation(dot.getDotno(), agv.getAgvId(), nextProcess, station.getSpecialline(), true);

        if (gbean == null) {
            boolean tocache = iStation.findCache(CacheStationEnumType.TH, agv.getAgvId(), true, true);
            if (tocache) {
                //去390
                toQr(agv, dot, nextProcess, CacheStationEnumType.TH.getName(), 161, station.getSpecialline(), station, stationEvent, true);
            } else {
                MsgCore.sendMsg(agv.getAgvId(), String.format("%s没有可用的机台，等待中", nextProcess));
            }
        } else {
            //没有特殊路线，但有定线
            toQr(agv, dot, nextProcess, gbean.getStationname(), 0, gbean.getSpecialline(), station, stationEvent, false);
        }
    }

    /**
     * BOE上料，只需要走路由
     *
     * @param dot
     * @param agv
     * @param station
     * @param stationEvent
     * @param amount
     */
    void leaveBOES(DotBean dot, AgvBean agv, StationBean station, IStationProtocol stationEvent, AgvMountBean amount) {
        //没有特殊路线，也没有定线
        toQr(agv, dot, "BOE下料", "", 0, 0, station, stationEvent, false);
    }

    /**
     * BOE下料，锁ALD湿法并走路由，否则不动
     *
     * @param dot
     * @param agv
     * @param station
     * @param stationEvent
     * @param amount
     */
    void leaveBOEX(DotBean dot, AgvBean agv, StationBean station, IStationProtocol stationEvent, AgvMountBean amount) {
        String nextProcess = "ALD湿法";
        StationGroupDao gbean = iStation.fetchNextInStation(dot.getDotno(), agv.getAgvId(), nextProcess, station.getSpecialline(), true);

        if (gbean == null) {
            // 没有可用的下料机台
            MsgCore.sendMsg(agv.getAgvId(), String.format("%s没有可用的机台，等待中", nextProcess));
        } else {
            //没有特殊路线，但有定线，去451
            toQr(agv, dot, nextProcess, station.getStationname(), 0, gbean.getSpecialline(), station, stationEvent, false);
        }
    }

    /**
     * 背膜上料，找背锁下料并直接去，如果没有则走路由返空
     *
     * @param dot
     * @param agv
     * @param station
     * @param stationEvent
     * @param amount
     */
    void leaveBMS(DotBean dot, AgvBean agv, StationBean station, IStationProtocol stationEvent, AgvMountBean amount) {
        //背膜上需要立即解锁，特殊的机台
        iStation.unlockStationForDot(dot.getDotno(), agv.getAgvId());

        String nextProcess = "背膜下料";
        StationGroupDao gbean = iStation.fetchNextOutStation(station.getDotno(), agv.getAgvId(), nextProcess, true);
        if (gbean == null) {
            // 没有可用的下料机台，退回二维码,让二维码去处理
            //没有特殊路线，也没有定线
            toQr(agv, dot, "", "", 0, 0, station, stationEvent, false);
        } else {
            RoadDao road = iRoad.getTransRoad(475, gbean.getDotno());
            if (road != null && road.getId() > 0) {
                //特殊路线，但没有定线
                toQr(agv, dot, nextProcess, gbean.getStationname(), road.getId(), 0, station, stationEvent, false);
            } else {
                MsgCore.sendMsg(agv.getAgvId(), String.format("%d到%d缺少路线", 475, gbean.getDotno()));
            }
        }
    }

    /**
     * 背膜下料，直接走路由
     *
     * @param dot
     * @param agv
     * @param station
     * @param stationEvent
     * @param amount
     */
    void leaveBMX(DotBean dot, AgvBean agv, StationBean station, IStationProtocol stationEvent, AgvMountBean amount) {
        String nextProcess = "丝网";
        StationGroupDao gbean = iStation.fetchNextInStation(dot.getDotno(), agv.getAgvId(), nextProcess, station.getSpecialline(), true);

        if (gbean != null) {
            //有机台，锁定， 没有特殊路线， 有定线，去476
            toQr(agv, dot, nextProcess, gbean.getStationname(), 0, gbean.getSpecialline(), station, stationEvent, false);
        } else {
            // 没有可用的下料机台
            // MsgCore.sendMsg(agv.getAgvId(), String.format("%s没有可用的机台，等待中", nextProcess));
            toQr(agv, dot, nextProcess, "", 0, station.getSpecialline(), station, stationEvent, false);
        }

    }

    /**
     * 正膜上料，找正膜下料，如果有则直接去，否则路由返空
     *
     * @param dot
     * @param agv
     * @param station
     * @param stationEventt
     * @param amount
     */
    void leaveZMS(DotBean dot, AgvBean agv, StationBean station, IStationProtocol stationEvent, AgvMountBean amount) {
        //正膜上需要立即解锁，特殊的机台
        iStation.unlockStationForDot(dot.getDotno(), agv.getAgvId());


        List<String> processList = Arrays.asList("背膜下料", "正膜下料");
        StationGroupDao gbean = iStation.fetchNextOutStation(station.getDotno(), agv.getAgvId(), processList, true);

        if (gbean == null) {
            // 没有可用的下料机台，退回二维码,让二维码去处理
            //空车，没有特殊路线，也没有定线
            toQr(agv, dot, "", "", 0, 0, station, stationEvent, false);
        } else {
            // 锁目标机台，然后走路由

            RoadDao road = iRoad.getTransRoad(475, gbean.getDotno());
            if (road != null && road.getId() > 0) {
                toQr(agv, dot, gbean.getProcess(), gbean.getStationname(), road.getId(), gbean.getSpecialline(), station, stationEvent, false);
            } else {
                MsgCore.sendMsg(agv.getAgvId(), String.format("%d到%d缺少路线", 475, gbean.getDotno()));
            }
        }
    }

    /**
     * 正膜下料，找背膜上料，如果有则去，否则等
     *
     * @param dot
     * @param agv
     * @param station
     * @param stationEvent
     * @param amount
     */
    void leaveZMX(DotBean dot, AgvBean agv, StationBean station, IStationProtocol stationEvent, AgvMountBean amount) {
        String nextProcess = "背膜上料";
        StationGroupDao gbean = iStation.fetchNextInStation(dot.getDotno(), agv.getAgvId(), nextProcess, station.getSpecialline(), true);

        if (gbean == null) {
            // 没有可用的下料机台，退回二维码,让二维码去处理
            boolean flag = iStation.findCache(CacheStationEnumType.ZMX, agv.getAgvId(), true, true);
            if (flag) {
                RoadDao road = iRoad.getTransRoad(dot.getDotno() - 1, CacheStationEnumType.ZMX.getDotno());
                if (road != null && road.getId() > 0) {
                    //有路才锁定
                    toQr(agv, dot, nextProcess, CacheStationEnumType.ZMX.getName(), road.getId(), station.getSpecialline(), station, stationEvent, false);
                } else {
                    MsgCore.sendMsg(agv.getAgvId(), String.format("%d与%d没有路线", 406, CacheStationEnumType.ZMX.getDotno()));
                }
            } else {
                MsgCore.sendMsg(agv.getAgvId(), String.format("%s没有可用的机台，等待中", nextProcess));
            }
        } else {
            // 锁目标机台，然后走路由

            RoadDao road = iRoad.getTransRoad(475, gbean.getDotno());
            if (road != null && road.getId() > 0) {
                toQr(agv, dot, nextProcess, gbean.getStationname(), road.getId(), gbean.getSpecialline(), station, stationEvent, false);
            } else {
                MsgCore.sendMsg(agv.getAgvId(), String.format("%d到%d缺少路线", 475, gbean.getDotno()));
            }
        }
    }

    /**
     * 离开丝网去ALD干法，如果没有，则等
     *
     * @param dot
     * @param agv
     * @param station
     * @param stationEvent
     * @param amount
     */
    void leaveSW(DotBean dot, AgvBean agv, StationBean station, IStationProtocol stationEvent, AgvMountBean amount) {
        //从473点开始的路线
        int startdot = 473;
        if (amount.getMountType() == AgvMountTypeEnum.AllEmpty) {
            //可以去ALD拿料
            List<String> processList = Arrays.asList("ALD干法");
            StationGroupDao gbean = iStation.fetchNextOutStation(station.getDotno(), agv.getAgvId(), processList, true);
            if (gbean == null) {
                //离开丝网,去452
                toQr(agv, dot, "", "", 0, 0, station, stationEvent, false);
            } else {
                //机台到机台的路线
                RoadDao road = iRoad.getTransRoad(startdot, gbean.getDotno());
                if (road != null && road.getId() > 0) {
                    //空车，有特殊路线，也没有定线
                    toQr(agv, dot, gbean.getProcess(), gbean.getStationname(), road.getId(), 0, station, stationEvent, false);
                } else {
                    MsgCore.sendMsg(agv.getAgvId(), String.format("%d到%d缺少路线", 476, gbean.getDotno()));
                }
            }
        } else {
            //只能去干法
            String nextProcess = "ALD干法";
            //无线别，空花篮
            StationGroupDao gbean = iStation.fetchNextInStation(dot.getDotno(), agv.getAgvId(), nextProcess, station.getSpecialline(), true);


            if (gbean == null) {
                MsgCore.sendMsg(agv.getAgvId(), "ALD干法没有可用的机台");
            } else {
                RoadDao road = iRoad.getTransRoad(startdot, gbean.getDotno());
                if (road != null && road.getId() > 0) {
                    //有特殊路线，但没有定线
                    toQr(agv, dot, nextProcess, gbean.getStationname(), 476, 0, station, stationEvent, false);
                } else {
                    MsgCore.sendMsg(agv.getAgvId(), String.format("%d到%d缺少路线", dot.getDotno(), gbean.getDotno()));
                }
            }
        }
    }

    /**
     * ALD湿法，走路由
     *
     * @param dot
     * @param agv
     * @param station
     * @param stationEvent
     * @param amount
     */
    void leaveALDS(DotBean dot, AgvBean agv, StationBean station, IStationProtocol stationEvent, AgvMountBean amount) {
        //TODO 找缓存机
        if (amount.getMountType() == AgvMountTypeEnum.AllEmpty) {
            // 只上料 空车
            toQr(agv, dot, "", "", 0, 0, station, stationEvent, false);
        } else {
            //直接离开机台
            toQr(agv, dot, "碱抛上料", "", 0, 0, station, stationEvent, false);

        }

    }

    /**
     * ALD干法，走路由
     *
     * @param dot
     * @param agv
     * @param station
     * @param stationEvent
     * @param amount
     */
    void leaveALDG(DotBean dot, AgvBean agv, StationBean station, IStationProtocol stationEvent, AgvMountBean amount) {
        //TODO 找缓存机
        if (amount.getMountType() == AgvMountTypeEnum.AllEmpty) {
            // 只上料
            //没有特殊路线，也没有定线
            toQr(agv, dot, "", "", 0, 0, station, stationEvent, false);
        } else {

            //直接去正膜 上料，不在机台等
            toQr(agv, dot, "正膜上料", "去正膜上料", 0, station.getSpecialline(), station, stationEvent, false);
        }

    }

    /**
     * 拆包机到发料上口
     *
     * @param dot
     * @param agv
     * @param station
     * @param stationEvent
     * @param amount
     */
    void leaveCB(DotBean dot, AgvBean agv, StationBean station, IStationProtocol stationEvent, AgvMountBean amount) {

        String nextProcess = "发料上料";
        StationGroupDao gbean = iStation.fetchNextInStation(dot.getDotno(), agv.getAgvId(), nextProcess, 0, true);


        if (gbean == null) {
            MsgCore.sendMsg(agv.getAgvId(), "发料上料没有可用的机台");
        } else {
            //没有特殊路线，也没有定线
            //注意，这里是二维码直接上机台的路线
            RoadDao road = iRoad.getTransRoad(201, gbean.getDotno());
            if (road != null && road.getId() > 0) {
                //有特殊路线，但没有定线
                toQr(agv, dot, nextProcess, gbean.getStationname(), road.getId(), 0, station, stationEvent, false);
            } else {
                MsgCore.sendMsg(agv.getAgvId(), String.format("%d到%d缺少路线", 201, gbean.getDotno()));
            }
        }


    }

}
